<template>
  <div class="app-container" style="background-color: #FFFFFF">

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
            type="primary"
            plain
            icon="Plus"
            @click="handleAdd"
            v-hasPermi="['keyan:followup:add']"
        >新增
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="success"
            plain
            icon="Edit"
            :disabled="single"
            @click="handleUpdate"
            v-hasPermi="['keyan:followup:edit']"
        >修改
        </el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
            type="danger"
            plain
            icon="Delete"
            :disabled="multiple"
            @click="handleDelete"
            v-hasPermi="['keyan:followup:remove']"
        >删除
        </el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="followupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center"/>
      <el-table-column label="回访时间" align="center" prop="followupTime" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.followupTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否患结直肠癌" align="center" prop="sfZjca">
        <template #default="scope">
          <dict-tag :options="ky_sf" :value="scope.row.sfZjca"/>
        </template>
      </el-table-column>
      <el-table-column label="结直肠癌确诊时间" align="center" prop="zjcaDate" width="180">
        <template #default="scope">
          <span>{{ parseTime(scope.row.zjcaDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="是否患有其它癌症" align="center" prop="sfQtaz">
        <template #default="scope">
          <dict-tag :options="ky_sf" :value="scope.row.sfQtaz"/>
        </template>
      </el-table-column>
      <el-table-column label="其它癌症名称" align="center" prop="azmc"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)"
                     v-hasPermi="['keyan:followup:edit']">修改
          </el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)"
                     v-hasPermi="['keyan:followup:remove']">删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
        v-show="total>0"
        :total="total"
        v-model:page="queryParams.pageNum"
        v-model:limit="queryParams.pageSize"
        @pagination="getList"
    />

    <!-- 添加或修改参与者回访对话框 -->
    <el-dialog :title="title" v-model="open" width="800px" style="min-height: 300px;padding: 30px" append-to-body>
      <el-form ref="followupRef" :model="form" :rules="rules" label-width="140px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="回访时间" prop="followupTime">
              <el-date-picker clearable
                              v-model="form.followupTime"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择回访时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">
            <el-form-item label="是否患结直肠癌" prop="sfZjca">
              <el-select v-model="form.sfZjca" placeholder="请选择是否患结直肠癌">
                <el-option
                    v-for="dict in ky_sf"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>

          <el-col :span="16">
            <el-form-item label="结直肠癌确诊时间" prop="zjcaDate">
              <el-date-picker clearable
                              v-model="form.zjcaDate"
                              type="date"
                              value-format="YYYY-MM-DD"
                              placeholder="请选择结直肠癌确诊时间">
              </el-date-picker>
            </el-form-item>
          </el-col>
        </el-row>
        <el-row>
          <el-col :span="8">

            <el-form-item label="是否患有其它癌症" prop="sfQtaz">
              <el-select v-model="form.sfQtaz" placeholder="请选择是否患有其它癌症">
                <el-option
                    v-for="dict in ky_sf"
                    :key="dict.value"
                    :label="dict.label"
                    :value="dict.value"
                ></el-option>
              </el-select>
            </el-form-item>
          </el-col>
          <el-col :span="16">

            <el-form-item label="其它癌症名称" prop="azmc">
              <el-input v-model="form.azmc" placeholder="请输入其它癌症名称"/>
            </el-form-item>
          </el-col>
        </el-row>


      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Followup">
const {proxy} = getCurrentInstance();
import {addFollowup, delFollowup, getFollowup, listFollowup, updateFollowup} from "@/api/keyan/followup";

const {ky_yw, ky_jcjb, ky_hrjgl, ky_sf, ky_yjqk, ky_yyxjg, ky_xys, ky_jcjg, sys_user_sex} =
    proxy.useDict("ky_yw", "ky_jcjb", "ky_hrjgl", "ky_sf", "ky_yjqk", "ky_yyxjg", "ky_xys", "ky_jcjg", "sys_user_sex");


const followupList = ref([]);
const open = ref(false);
const loading = ref(false);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");


const props = defineProps({
  patientId: {
    type: String,
    default: null,
  },
});

const data = reactive({
  form: {
    patientId: props.patientId,
  },
  queryParams: {
    pageNum: 1,
    pageSize: 10,
  },
  rules: {
    followupTime: [
      {required: true, message: "回访时间不能为空", trigger: "blur"}
    ],
    sfZjca: [
      {required: true, message: "是否患结直肠癌，0：未患，1：患有不能为空", trigger: "change"}
    ],
    sfQtaz: [
      {required: true, message: "是否患有其它癌症，0否，1有不能为空", trigger: "change"}
    ],
  }
});

const {queryParams, form, rules} = toRefs(data);


watch(props, (newPatientId) => {
  getList();
});

/** 查询参与者回访列表 */
function getList() {
  queryParams.value.patientId = props.patientId;
  // 如果patientID不为空再查询
  if (queryParams.value.patientId) {
    loading.value = true;
    listFollowup(queryParams.value).then(response => {
      followupList.value = response.rows;
    }).finally(() => {
      loading.value = false;
    });

  }
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    id: null,
    patientId: props.patientId,
    followupTime: null,
    sfZjca: null,
    zjcaDate: null,
    sfQtaz: null,
    azmc: null,
    createBy: null,
    createTime: null
  };
  proxy.resetForm("followupRef");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.id);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加参与者回访";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _id = row.id || ids.value
  getFollowup(_id).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改参与者回访";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["followupRef"].validate(valid => {
    if (valid) {
      form.value.patientId = props.patientId;
      if (form.value.id != null) {
        updateFollowup(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addFollowup(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _ids = row.id || ids.value;
  proxy.$modal.confirm('是否确认删除参与者回访编号为"' + _ids + '"的数据项？').then(function () {
    return delFollowup(_ids);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {
  });
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('keyan/followup/export', {
    ...queryParams.value
  }, `followup_${new Date().getTime()}.xlsx`)
}

onMounted(
    () => {
      getList();
    }
);
</script>

<style lang="scss" scoped>
.app-container {
  background: #FFFFFF;
}
</style>